package com.bdcom.autotest.telnet.util.ping;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPV6Util {
	/**
	 * 将 非简写的IPv6 转换成 简写的IPv6
	 * 
	 * @param fullIPv6 非简写的IPv6
	 * @return 简写的IPv6
	 */
	public static String parseFullIPv6ToAbbreviation(String fullIPv6) {
		String abbreviation = "";

		// 1,校验 ":" 的个数 不等于7  或者长度不等于39  直接返回空串
		int count = fullIPv6.length() - fullIPv6.replaceAll(":", "").length();
		if (fullIPv6.length() != 39 || count != 7) {
			//return abbreviation;
			fullIPv6=completionFullIpv6(fullIPv6);
		}

// 2,去掉每一位前面的0
		String[] arr = fullIPv6.split(":");

		for (int i = 0; i < arr.length; i++) {
			arr[i] = arr[i].replaceAll("^0{1,3}", "");
		}

		// 3,找到最长的连续的0
		String[] arr2 = arr.clone();
		for (int i = 0; i < arr2.length; i++) {
			if (!"0".equals(arr2[i])) {
				arr2[i] = "-";
			}
		}

		Pattern pattern = Pattern.compile("0{2,}");
		Matcher matcher = pattern.matcher(StringUtils.join(arr2, ""));
		String maxStr = "";
		int start = -1;
		int end = -1;
		while (matcher.find()) {
			if (maxStr.length() < matcher.group().length()) {
				maxStr = matcher.group();
				start = matcher.start();
				end = matcher.end();
			}
		}

		// 3,合并        
		if (maxStr.length() > 0) {
			for (int i = start; i < end; i++) {
				arr[i] = ":";
			}
		}
		abbreviation = StringUtils.join(arr, ":");
		abbreviation = abbreviation.replaceAll(":{2,}", "::");

		return abbreviation;
	}

	/**
	 * 将 简写的IPv6 转换成 非简写的IPv6      *      
	 * @param abbreviation 简写的IPv6     
	 * * @return 非简写的IPv6     
	 */
	public static String parseAbbreviationToFullIPv6(String abbreviation) {
		String fullIPv6 = "";

		if ("::".equals(abbreviation)) {
			return "0000:0000:0000:0000:0000:0000:0000:0000";
		}

		String[] arr = new String[] { "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000" };

		if (abbreviation.startsWith("::")) {
			String[] temp = abbreviation.substring(2, abbreviation.length()).split(":");
			for (int i = 0; i < temp.length; i++) {
				String tempStr = "0000" + temp[i];
				arr[i + 8 - temp.length] = tempStr.substring(tempStr.length() - 4);
			}

		} else if (abbreviation.endsWith("::")) {
			String[] temp = abbreviation.substring(0, abbreviation.length() - 2).split(":");
			for (int i = 0; i < temp.length; i++) {
				String tempStr = "0000" + temp[i];
				arr[i] = tempStr.substring(tempStr.length() - 4);
			}

		} else if (abbreviation.contains("::")) {
			String[] tempArr = abbreviation.split("::");

			String[] temp0 = tempArr[0].split(":");
			for (int i = 0; i < temp0.length; i++) {
				String tempStr = "0000" + temp0[i];
				arr[i] = tempStr.substring(tempStr.length() - 4);
			}

			String[] temp1 = tempArr[1].split(":");
			for (int i = 0; i < temp1.length; i++) {
				String tempStr = "0000" + temp1[i];
				arr[i + 8 - temp1.length] = tempStr.substring(tempStr.length() - 4);
			}

		} else {
			String[] tempArr = abbreviation.split(":");

			for (int i = 0; i < tempArr.length; i++) {
				String tempStr = "0000" + tempArr[i];
				arr[i] = tempStr.substring(tempStr.length() - 4);
			}

		}

		fullIPv6 = StringUtils.join(arr, ":");

		return fullIPv6;

	}
	public static String completionFullIpv6(String ipv6)
	{
		String ipv6Array[]=ipv6.split(":");
		for (int i = 0; i < ipv6Array.length; i++) {
			if(ipv6Array[i].length()==1) {
				ipv6Array[i]="000"+ipv6Array[i];
			}
			if(ipv6Array[i].length()==2) {
				ipv6Array[i]="00"+ipv6Array[i];
			}
			if(ipv6Array[i].length()==3) {
				ipv6Array[i]="0"+ipv6Array[i];
			}
		}
		return StringUtils.join(ipv6Array,":");
	}
	
public static void main(String[] args) {
	//String ipv6full="1111:0:0:0222:0:0:0:3333";
	//String x=parseFullIPv6ToAbbreviation(ipv6full);
	//System.out.println("缩写："+x);
	//String full=parseAbbreviationToFullIPv6(x);
	//System.out.println("全写："+full);
	String inDBIpv6=convert2DBIPV6FromBDCOM("1111:0123:01:0222:0:0:0:FFFF");
	inDBIpv6=convert2DBIPV6FromBDCOM("1111:012::222:0:0:0:FFFF");
	System.out.println(inDBIpv6);
}
/**
 *  将博达格式的ipv6转换为数据库里的格式
 * @param bdcomIPV6   1111::222:0:0:0:FFFF
 * @return
 */
public static String convert2DBIPV6FromBDCOM(String bdcomIPV6)
{
	StringBuilder builder=new StringBuilder();
	if(bdcomIPV6.contains("::"))
	{
		
		String ipv6_1=bdcomIPV6.split("::")[0];
		String ipv6_2=bdcomIPV6.split("::")[1];
		int length1=ipv6_1.split(":").length;
		int length2=ipv6_2.split(":").length;
		int count0=8-length1-length2;
		bdcomIPV6=ipv6_1;
		for (int i = 0; i < count0; i++) {
			bdcomIPV6+=":0";
		}
		bdcomIPV6+=":"+ipv6_2;
		
	}else {
		
	}
	

	String arrayIpv6[]=bdcomIPV6.split(":");
	for (int i = 0; i < arrayIpv6.length; i++) {
		String string = arrayIpv6[i];
		
		if(!string.equals("0")) {
			if(i!=arrayIpv6.length-1) {
				builder.append(string.replaceAll("^0{1,3}", "")).append(":");
			}else {
				builder.append(string.replaceAll("^0{1,3}", ""));
			}
			
		}else {
			if(i!=arrayIpv6.length-1) {
			    builder.append("0").append(":");
			}
			else {
				builder.append("0");
			}
		}
	
		
	}
	
	
	

	
	
	return builder.toString();
	}
}
